import logging

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from rest_framework.authentication import TokenAuthentication
from rest_framework.views import APIView
from backend.common.api_response import JsonResponse
from backend.models import Lyzd_Encryption
from backend.serializers import SysLyzd_EncryptionSerializer

logger = logging.getLogger(__name__)  # 这里使用 __name__ 动态搜索定义的 logger 配置，这里有一个层次关系的知识点。

"""
加密解密

"""


class getLyzdEncryptions(APIView):
    authentication_classes = (TokenAuthentication,)
    permission_classes = ()

    def get(self, request):
        """
       获取加密解密数据list
        :param request:
        :return:
        """

        try:
            page_size = int(request.GET.get("page_size", 20))
            page = int(request.GET.get("page", 1))

        except (TypeError, ValueError):
            return JsonResponse(code="999985", msg="page and page_size must be integer!")

        project_name = request.GET.get("project_name")

        # interface_name_zh = request.GET.get("interface_name_zh")
        """获取所有project，去掉status=1的project"""
        # encrytion = Lyzd_Encrytion.objects.filter()
        """获取所有model，去掉status=1的model"""
        # model = Sys_Model.objects.filter(status=0)

        obi = Lyzd_Encryption.objects.all().order_by("id")

        paginator = Paginator(obi, page_size)  # paginator对象
        total = paginator.num_pages  # 总页数

        try:

            obm = paginator.page(page)

        except PageNotAnInteger:

            obm = paginator.page(1)
        except EmptyPage:

            obm = paginator.page(paginator.num_pages)
        serialize = SysLyzd_EncryptionSerializer(obm, many=True)

        return JsonResponse(data={"data": serialize.data,
                                  "page": page,
                                  "total": total
                                  }, code="999999", msg="成功")
